<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Remapping</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d9/df8/tutorial_root.html">OpenCV Tutorials</a></li><li class="navelem"><a class="el" href="../../d7/da8/tutorial_table_of_content_imgproc.html">Image Processing (imgproc module)</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Remapping </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>Prev Tutorial:</b> <a class="el" href="../../d4/d70/tutorial_hough_circle.html">Hough Circle Transform</a></p>
<p><b>Next Tutorial:</b> <a class="el" href="../../d4/d61/tutorial_warp_affine.html">Affine Transformations</a></p>
<table class="doxtable">
<tr>
<th align="right"></th><th align="left"></th></tr>
<tr>
<td align="right">Original author </td><td align="left">Ana Huamán </td></tr>
<tr>
<td align="right">Compatibility </td><td align="left">OpenCV &gt;= 3.0 </td></tr>
</table>
<h2>Goal </h2>
<p>In this tutorial you will learn how to:</p>
<p>a. Use the OpenCV function <a class="el" href="../../da/d54/group__imgproc__transform.html#gab75ef31ce5cdfb5c44b6da5f3b908ea4">cv::remap</a> to implement simple remapping routines.</p>
<h2>Theory </h2>
<h3>What is remapping?</h3>
<ul>
<li>It is the process of taking pixels from one place in the image and locating them in another position in a new image.</li>
<li>To accomplish the mapping process, it might be necessary to do some interpolation for non-integer pixel locations, since there will not always be a one-to-one-pixel correspondence between source and destination images.</li>
<li><p class="startli">We can express the remap for every pixel location \((x,y)\) as:</p>
<p class="formulaDsp">
\[g(x,y) = f ( h(x,y) )\]
</p>
<p class="startli">where \(g()\) is the remapped image, \(f()\) the source image and \(h(x,y)\) is the mapping function that operates on \((x,y)\).</p>
</li>
<li><p class="startli">Let's think in a quick example. Imagine that we have an image \(I\) and, say, we want to do a remap such that:</p>
<p class="formulaDsp">
\[h(x,y) = (I.cols - x, y )\]
</p>
<p class="startli">What would happen? It is easily seen that the image would flip in the \(x\) direction. For instance, consider the input image:</p>
<div class="image">
<img src="../../Remap_Tutorial_Theory_0.jpg" alt="Remap_Tutorial_Theory_0.jpg"/>
</div>
<p class="startli">observe how the red circle changes positions with respect to x (considering \(x\) the horizontal direction):</p>
<div class="image">
<img src="../../Remap_Tutorial_Theory_1.jpg" alt="Remap_Tutorial_Theory_1.jpg"/>
</div>
</li>
<li>In OpenCV, the function <a class="el" href="../../da/d54/group__imgproc__transform.html#gab75ef31ce5cdfb5c44b6da5f3b908ea4">cv::remap</a> offers a simple remapping implementation.</li>
</ul>
<h2>Code </h2>
<ul>
<li><b>What does this program do?</b><ul>
<li>Loads an image</li>
<li>Each second, apply 1 of 4 different remapping processes to the image and display them indefinitely in a window.</li>
<li>Wait for the user to exit the program</li>
</ul>
</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'><ul>
<li>The tutorial code's is shown lines below. You can also download it from <a href="https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/ImgTrans/Remap_Demo.cpp">here</a> <div class="fragment"><div class="line"></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d6/d87/imgcodecs_8hpp.html">opencv2/imgcodecs.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d1/d4f/imgproc_2include_2opencv2_2imgproc_8hpp.html">opencv2/imgproc.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> update_map( <span class="keywordtype">int</span> &amp;ind, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> &amp;map_x, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> &amp;map_y );</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keyword">const</span> <span class="keywordtype">char</span>** argv)</div><div class="line">{</div><div class="line">    <a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html">CommandLineParser</a> parser(argc, argv, <span class="stringliteral">&quot;{@image |chicky_512.png|input image name}&quot;</span>);</div><div class="line">    std::string filename = parser.get&lt;std::string&gt;(0);</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> src = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>( <a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">samples::findFile</a>( filename ), <a class="code" href="../../d8/d6a/group__imgcodecs__flags.html#gga61d9b0126a3e57d9277ac48327799c80af660544735200cbe942eea09232eb822">IMREAD_COLOR</a> );</div><div class="line">    <span class="keywordflow">if</span> (src.empty())</div><div class="line">    {</div><div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;Cannot read image: &quot;</span> &lt;&lt; filename &lt;&lt; std::endl;</div><div class="line">        <span class="keywordflow">return</span> -1;</div><div class="line">    }</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> dst(src.size(), src.type());</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> map_x(src.size(), <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga32ec76240e43e4c9c7b2e2785180a7e6">CV_32FC1</a>);</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> map_y(src.size(), <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga32ec76240e43e4c9c7b2e2785180a7e6">CV_32FC1</a>);</div><div class="line"></div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">char</span>* remap_window = <span class="stringliteral">&quot;Remap demo&quot;</span>;</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">namedWindow</a>( remap_window, <a class="code" href="../../d0/d90/group__highgui__window__flags.html#ggabf7d2c5625bc59ac130287f925557ac3acf621ace7a54954cbac01df27e47228f">WINDOW_AUTOSIZE</a> );</div><div class="line"></div><div class="line">    <span class="keywordtype">int</span> ind = 0;</div><div class="line">    <span class="keywordflow">for</span>(;;)</div><div class="line">    {</div><div class="line">        update_map(ind, map_x, map_y);</div><div class="line">        <a class="code" href="../../da/d54/group__imgproc__transform.html#gab75ef31ce5cdfb5c44b6da5f3b908ea4">remap</a>( src, dst, map_x, map_y, <a class="code" href="../../da/d54/group__imgproc__transform.html#gga5bb5a1fea74ea38e1a5445ca803ff121ac97d8e4880d8b5d509e96825c7522deb">INTER_LINEAR</a>, <a class="code" href="../../d2/de8/group__core__array.html#gga209f2f4869e304c82d07739337eae7c5aed2e4346047e265c8c5a6d0276dcd838">BORDER_CONSTANT</a>, <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0, 0, 0) );</div><div class="line"></div><div class="line">        <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>( remap_window, dst );</div><div class="line"></div><div class="line">        <span class="keywordtype">char</span> c = (char)<a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>( 1000 );</div><div class="line">        <span class="keywordflow">if</span>( c == 27 )</div><div class="line">        {</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        }</div><div class="line">    }</div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> update_map( <span class="keywordtype">int</span> &amp;ind, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> &amp;map_x, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> &amp;map_y )</div><div class="line">{</div><div class="line">    <span class="keywordflow">for</span>( <span class="keywordtype">int</span> i = 0; i &lt; map_x.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>; i++ )</div><div class="line">    {</div><div class="line">        <span class="keywordflow">for</span>( <span class="keywordtype">int</span> j = 0; j &lt; map_x.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>; j++ )</div><div class="line">        {</div><div class="line">            <span class="keywordflow">switch</span>( ind )</div><div class="line">            {</div><div class="line">            <span class="keywordflow">case</span> 0:</div><div class="line">                <span class="keywordflow">if</span>( j &gt; map_x.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>*0.25 &amp;&amp; j &lt; map_x.cols*0.75 &amp;&amp; i &gt; map_x.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>*0.25 &amp;&amp; i &lt; map_x.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>*0.75 )</div><div class="line">                {</div><div class="line">                    map_x.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<span class="keywordtype">float</span>&gt;(i, j) = 2*( j - map_x.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>*0.25f ) + 0.5f;</div><div class="line">                    map_y.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<span class="keywordtype">float</span>&gt;(i, j) = 2*( i - map_x.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>*0.25f ) + 0.5f;</div><div class="line">                }</div><div class="line">                <span class="keywordflow">else</span></div><div class="line">                {</div><div class="line">                    map_x.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<span class="keywordtype">float</span>&gt;(i, j) = 0;</div><div class="line">                    map_y.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<span class="keywordtype">float</span>&gt;(i, j) = 0;</div><div class="line">                }</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">            <span class="keywordflow">case</span> 1:</div><div class="line">                map_x.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<span class="keywordtype">float</span>&gt;(i, j) = (<span class="keywordtype">float</span>)j;</div><div class="line">                map_y.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<span class="keywordtype">float</span>&gt;(i, j) = (<span class="keywordtype">float</span>)(map_x.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a> - i);</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">            <span class="keywordflow">case</span> 2:</div><div class="line">                map_x.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<span class="keywordtype">float</span>&gt;(i, j) = (<span class="keywordtype">float</span>)(map_x.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a> - j);</div><div class="line">                map_y.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<span class="keywordtype">float</span>&gt;(i, j) = (<span class="keywordtype">float</span>)i;</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">            <span class="keywordflow">case</span> 3:</div><div class="line">                map_x.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<span class="keywordtype">float</span>&gt;(i, j) = (<span class="keywordtype">float</span>)(map_x.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a> - j);</div><div class="line">                map_y.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<span class="keywordtype">float</span>&gt;(i, j) = (<span class="keywordtype">float</span>)(map_x.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a> - i);</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">            <span class="keywordflow">default</span>:</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">            } <span class="comment">// end of switch</span></div><div class="line">        }</div><div class="line">    }</div><div class="line">    ind = (ind+1) % 4;</div><div class="line">}</div></div><!-- fragment -->  </div> </li>
</ul>
 <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'><ul>
<li>The tutorial code's is shown lines below. You can also download it from <a href="https://github.com/opencv/opencv/tree/master/samples/java/tutorial_code/ImgTrans/remap/RemapDemo.java">here</a> <div class="fragment"><div class="line"><span class="keyword">import</span> org.opencv.core.Core;</div><div class="line"><span class="keyword">import</span> org.opencv.core.CvType;</div><div class="line"><span class="keyword">import</span> org.opencv.core.Mat;</div><div class="line"><span class="keyword">import</span> org.opencv.highgui.HighGui;</div><div class="line"><span class="keyword">import</span> org.opencv.imgcodecs.Imgcodecs;</div><div class="line"><span class="keyword">import</span> org.opencv.imgproc.Imgproc;</div><div class="line"></div><div class="line"><span class="keyword">class </span>Remap {</div><div class="line">    <span class="keyword">private</span> Mat mapX = <span class="keyword">new</span> Mat();</div><div class="line">    <span class="keyword">private</span> Mat mapY = <span class="keyword">new</span> Mat();</div><div class="line">    <span class="keyword">private</span> Mat dst = <span class="keyword">new</span> Mat();</div><div class="line">    <span class="keyword">private</span> <span class="keywordtype">int</span> ind = 0;</div><div class="line"></div><div class="line">    <span class="keyword">private</span> <span class="keywordtype">void</span> updateMap() {</div><div class="line">        <span class="keywordtype">float</span> buffX[] = <span class="keyword">new</span> <span class="keywordtype">float</span>[(int) (mapX.total() * mapX.channels())];</div><div class="line">        mapX.get(0, 0, buffX);</div><div class="line"></div><div class="line">        <span class="keywordtype">float</span> buffY[] = <span class="keyword">new</span> <span class="keywordtype">float</span>[(int) (mapY.total() * mapY.channels())];</div><div class="line">        mapY.get(0, 0, buffY);</div><div class="line"></div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; mapX.rows(); i++) {</div><div class="line">            <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j &lt; mapX.cols(); j++) {</div><div class="line">                <span class="keywordflow">switch</span> (ind) {</div><div class="line">                <span class="keywordflow">case</span> 0:</div><div class="line">                    <span class="keywordflow">if</span>( j &gt; mapX.cols()*0.25 &amp;&amp; j &lt; mapX.cols()*0.75 &amp;&amp; i &gt; mapX.rows()*0.25 &amp;&amp; i &lt; mapX.rows()*0.75 ) {</div><div class="line">                        buffX[i*mapX.cols() + j] = 2*( j - mapX.cols()*0.25f ) + 0.5f;</div><div class="line">                        buffY[i*mapY.cols() + j] = 2*( i - mapX.rows()*0.25f ) + 0.5f;</div><div class="line">                    } <span class="keywordflow">else</span> {</div><div class="line">                        buffX[i*mapX.cols() + j] = 0;</div><div class="line">                        buffY[i*mapY.cols() + j] = 0;</div><div class="line">                    }</div><div class="line">                    <span class="keywordflow">break</span>;</div><div class="line">                <span class="keywordflow">case</span> 1:</div><div class="line">                    buffX[i*mapX.cols() + j] = j;</div><div class="line">                    buffY[i*mapY.cols() + j] = mapY.rows() - i;</div><div class="line">                    <span class="keywordflow">break</span>;</div><div class="line">                <span class="keywordflow">case</span> 2:</div><div class="line">                    buffX[i*mapX.cols() + j] = mapY.cols() - j;</div><div class="line">                    buffY[i*mapY.cols() + j] = i;</div><div class="line">                    <span class="keywordflow">break</span>;</div><div class="line">                <span class="keywordflow">case</span> 3:</div><div class="line">                    buffX[i*mapX.cols() + j] = mapY.cols() - j;</div><div class="line">                    buffY[i*mapY.cols() + j] = mapY.rows() - i;</div><div class="line">                    <span class="keywordflow">break</span>;</div><div class="line">                <span class="keywordflow">default</span>:</div><div class="line">                    <span class="keywordflow">break</span>;</div><div class="line">                }</div><div class="line">            }</div><div class="line">        }</div><div class="line">        mapX.put(0, 0, buffX);</div><div class="line">        mapY.put(0, 0, buffY);</div><div class="line">        ind = (ind+1) % 4;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keyword">public</span> <span class="keywordtype">void</span> run(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> filename = args.length &gt; 0 ? args[0] : <span class="stringliteral">&quot;../data/chicky_512.png&quot;</span>;</div><div class="line">        Mat src = Imgcodecs.imread(filename, Imgcodecs.IMREAD_COLOR);</div><div class="line">        <span class="keywordflow">if</span> (src.empty()) {</div><div class="line">            System.err.println(<span class="stringliteral">&quot;Cannot read image: &quot;</span> + filename);</div><div class="line">            System.exit(0);</div><div class="line">        }</div><div class="line"></div><div class="line">        mapX = <span class="keyword">new</span> Mat(src.size(), CvType.CV_32F);</div><div class="line">        mapY = <span class="keyword">new</span> Mat(src.size(), CvType.CV_32F);</div><div class="line"></div><div class="line">        <span class="keyword">final</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> winname = <span class="stringliteral">&quot;Remap demo&quot;</span>;</div><div class="line">        HighGui.namedWindow(winname, HighGui.WINDOW_AUTOSIZE);</div><div class="line"></div><div class="line">        <span class="keywordflow">for</span> (;;) {</div><div class="line">            updateMap();</div><div class="line">            Imgproc.remap(src, dst, mapX, mapY, Imgproc.INTER_LINEAR);</div><div class="line">            HighGui.imshow(winname, dst);</div><div class="line">            <span class="keywordflow">if</span> (HighGui.waitKey(1000) == 27) {</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">            }</div><div class="line">        }</div><div class="line">        System.exit(0);</div><div class="line">    }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="keyword">class </span>RemapDemo {</div><div class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keywordtype">void</span> main(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">        <span class="comment">// Load the native OpenCV library</span></div><div class="line">        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);</div><div class="line"></div><div class="line">        <span class="keyword">new</span> Remap().run(args);</div><div class="line">    }</div><div class="line">}</div></div><!-- fragment -->  </div> </li>
</ul>
 <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'><ul>
<li>The tutorial code's is shown lines below. You can also download it from <a href="https://github.com/opencv/opencv/tree/master/samples/python/tutorial_code/ImgTrans/remap/Remap_Demo.py">here</a> <div class="fragment"><div class="line"><span class="keyword">from</span> __future__ <span class="keyword">import</span> print_function</div><div class="line"><span class="keyword">import</span> cv2 <span class="keyword">as</span> cv</div><div class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</div><div class="line"><span class="keyword">import</span> argparse</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">def </span>update_map(ind, map_x, map_y):</div><div class="line">    <span class="keywordflow">if</span> ind == 0:</div><div class="line">        <span class="keywordflow">for</span> i <span class="keywordflow">in</span> range(map_x.shape[0]):</div><div class="line">            <span class="keywordflow">for</span> j <span class="keywordflow">in</span> range(map_x.shape[1]):</div><div class="line">                <span class="keywordflow">if</span> j &gt; map_x.shape[1]*0.25 <span class="keywordflow">and</span> j &lt; map_x.shape[1]*0.75 <span class="keywordflow">and</span> i &gt; map_x.shape[0]*0.25 <span class="keywordflow">and</span> i &lt; map_x.shape[0]*0.75:</div><div class="line">                    map_x[i,j] = 2 * (j-map_x.shape[1]*0.25) + 0.5</div><div class="line">                    map_y[i,j] = 2 * (i-map_y.shape[0]*0.25) + 0.5</div><div class="line">                <span class="keywordflow">else</span>:</div><div class="line">                    map_x[i,j] = 0</div><div class="line">                    map_y[i,j] = 0</div><div class="line">    <span class="keywordflow">elif</span> ind == 1:</div><div class="line">        <span class="keywordflow">for</span> i <span class="keywordflow">in</span> range(map_x.shape[0]):</div><div class="line">            map_x[i,:] = [x <span class="keywordflow">for</span> x <span class="keywordflow">in</span> range(map_x.shape[1])]</div><div class="line">        <span class="keywordflow">for</span> j <span class="keywordflow">in</span> range(map_y.shape[1]):</div><div class="line">            map_y[:,j] = [map_y.shape[0]-y <span class="keywordflow">for</span> y <span class="keywordflow">in</span> range(map_y.shape[0])]</div><div class="line">    <span class="keywordflow">elif</span> ind == 2:</div><div class="line">        <span class="keywordflow">for</span> i <span class="keywordflow">in</span> range(map_x.shape[0]):</div><div class="line">            map_x[i,:] = [map_x.shape[1]-x <span class="keywordflow">for</span> x <span class="keywordflow">in</span> range(map_x.shape[1])]</div><div class="line">        <span class="keywordflow">for</span> j <span class="keywordflow">in</span> range(map_y.shape[1]):</div><div class="line">            map_y[:,j] = [y <span class="keywordflow">for</span> y <span class="keywordflow">in</span> range(map_y.shape[0])]</div><div class="line">    <span class="keywordflow">elif</span> ind == 3:</div><div class="line">        <span class="keywordflow">for</span> i <span class="keywordflow">in</span> range(map_x.shape[0]):</div><div class="line">            map_x[i,:] = [map_x.shape[1]-x <span class="keywordflow">for</span> x <span class="keywordflow">in</span> range(map_x.shape[1])]</div><div class="line">        <span class="keywordflow">for</span> j <span class="keywordflow">in</span> range(map_y.shape[1]):</div><div class="line">            map_y[:,j] = [map_y.shape[0]-y <span class="keywordflow">for</span> y <span class="keywordflow">in</span> range(map_y.shape[0])]</div><div class="line"></div><div class="line"></div><div class="line">parser = argparse.ArgumentParser(description=<span class="stringliteral">&#39;Code for Remapping tutorial.&#39;</span>)</div><div class="line">parser.add_argument(<span class="stringliteral">&#39;--input&#39;</span>, help=<span class="stringliteral">&#39;Path to input image.&#39;</span>, default=<span class="stringliteral">&#39;chicky_512.png&#39;</span>)</div><div class="line">args = parser.parse_args()</div><div class="line"></div><div class="line"></div><div class="line">src = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(<a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">cv.samples.findFile</a>(args.input), cv.IMREAD_COLOR)</div><div class="line"><span class="keywordflow">if</span> src <span class="keywordflow">is</span> <span class="keywordtype">None</span>:</div><div class="line">    <a class="code" href="../../df/d57/namespacecv_1_1dnn.html#a701210a0203f2786cbfd04b2bd56da47">print</a>(<span class="stringliteral">&#39;Could not open or find the image: &#39;</span>, args.input)</div><div class="line">    exit(0)</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">map_x = np.zeros((src.shape[0], src.shape[1]), dtype=np.float32)</div><div class="line">map_y = np.zeros((src.shape[0], src.shape[1]), dtype=np.float32)</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">window_name = <span class="stringliteral">&#39;Remap demo&#39;</span></div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">cv.namedWindow</a>(window_name)</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">ind = 0</div><div class="line"><span class="keywordflow">while</span> <span class="keyword">True</span>:</div><div class="line">    update_map(ind, map_x, map_y)</div><div class="line">    ind = (ind + 1) % 4</div><div class="line">    dst = <a class="code" href="../../da/d54/group__imgproc__transform.html#gab75ef31ce5cdfb5c44b6da5f3b908ea4">cv.remap</a>(src, map_x, map_y, cv.INTER_LINEAR)</div><div class="line">    <a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(window_name, dst)</div><div class="line">    c = <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">cv.waitKey</a>(1000)</div><div class="line">    <span class="keywordflow">if</span> c == 27:</div><div class="line">        <span class="keywordflow">break</span></div><div class="line"></div></div><!-- fragment -->  </div> </li>
</ul>
<h2>Explanation </h2>
<ul>
<li>Load an image:</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    Mat src = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>( <a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">samples::findFile</a>( filename ), <a class="code" href="../../d8/d6a/group__imgcodecs__flags.html#gga61d9b0126a3e57d9277ac48327799c80af660544735200cbe942eea09232eb822">IMREAD_COLOR</a> );</div><div class="line">    <span class="keywordflow">if</span> (src.empty())</div><div class="line">    {</div><div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;Cannot read image: &quot;</span> &lt;&lt; filename &lt;&lt; std::endl;</div><div class="line">        <span class="keywordflow">return</span> -1;</div><div class="line">    }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        Mat src = Imgcodecs.imread(filename, Imgcodecs.IMREAD_COLOR);</div><div class="line">        <span class="keywordflow">if</span> (src.empty()) {</div><div class="line">            System.err.println(<span class="stringliteral">&quot;Cannot read image: &quot;</span> + filename);</div><div class="line">            System.exit(0);</div><div class="line">        }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">src = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(<a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">cv.samples.findFile</a>(args.input), cv.IMREAD_COLOR)</div><div class="line"><span class="keywordflow">if</span> src <span class="keywordflow">is</span> <span class="keywordtype">None</span>:</div><div class="line">    <a class="code" href="../../df/d57/namespacecv_1_1dnn.html#a701210a0203f2786cbfd04b2bd56da47">print</a>(<span class="stringliteral">&#39;Could not open or find the image: &#39;</span>, args.input)</div><div class="line">    exit(0)</div></div><!-- fragment --> </div> <ul>
<li>Create the destination image and the two mapping matrices (for x and y )</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    Mat dst(src.size(), src.type());</div><div class="line">    Mat map_x(src.size(), <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga32ec76240e43e4c9c7b2e2785180a7e6">CV_32FC1</a>);</div><div class="line">    Mat map_y(src.size(), <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga32ec76240e43e4c9c7b2e2785180a7e6">CV_32FC1</a>);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        mapX = <span class="keyword">new</span> Mat(src.size(), CvType.CV_32F);</div><div class="line">        mapY = <span class="keyword">new</span> Mat(src.size(), CvType.CV_32F);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">map_x = np.zeros((src.shape[0], src.shape[1]), dtype=np.float32)</div><div class="line">map_y = np.zeros((src.shape[0], src.shape[1]), dtype=np.float32)</div></div><!-- fragment --> </div> <ul>
<li>Create a window to display results</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    <span class="keyword">const</span> <span class="keywordtype">char</span>* remap_window = <span class="stringliteral">&quot;Remap demo&quot;</span>;</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">namedWindow</a>( remap_window, <a class="code" href="../../d0/d90/group__highgui__window__flags.html#ggabf7d2c5625bc59ac130287f925557ac3acf621ace7a54954cbac01df27e47228f">WINDOW_AUTOSIZE</a> );</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        <span class="keyword">final</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> winname = <span class="stringliteral">&quot;Remap demo&quot;</span>;</div><div class="line">        HighGui.namedWindow(winname, HighGui.WINDOW_AUTOSIZE);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">window_name = <span class="stringliteral">&#39;Remap demo&#39;</span></div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">cv.namedWindow</a>(window_name)</div></div><!-- fragment --> </div> <ul>
<li>Establish a loop. Each 1000 ms we update our mapping matrices (<em>mat_x</em> and <em>mat_y</em>) and apply them to our source image:</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    <span class="keywordtype">int</span> ind = 0;</div><div class="line">    <span class="keywordflow">for</span>(;;)</div><div class="line">    {</div><div class="line">        update_map(ind, map_x, map_y);</div><div class="line">        <a class="code" href="../../da/d54/group__imgproc__transform.html#gab75ef31ce5cdfb5c44b6da5f3b908ea4">remap</a>( src, dst, map_x, map_y, <a class="code" href="../../da/d54/group__imgproc__transform.html#gga5bb5a1fea74ea38e1a5445ca803ff121ac97d8e4880d8b5d509e96825c7522deb">INTER_LINEAR</a>, <a class="code" href="../../d2/de8/group__core__array.html#gga209f2f4869e304c82d07739337eae7c5aed2e4346047e265c8c5a6d0276dcd838">BORDER_CONSTANT</a>, <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0, 0, 0) );</div><div class="line"></div><div class="line">        <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>( remap_window, dst );</div><div class="line"></div><div class="line">        <span class="keywordtype">char</span> c = (char)<a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>( 1000 );</div><div class="line">        <span class="keywordflow">if</span>( c == 27 )</div><div class="line">        {</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        }</div><div class="line">    }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        <span class="keywordflow">for</span> (;;) {</div><div class="line">            updateMap();</div><div class="line">            Imgproc.remap(src, dst, mapX, mapY, Imgproc.INTER_LINEAR);</div><div class="line">            HighGui.imshow(winname, dst);</div><div class="line">            <span class="keywordflow">if</span> (HighGui.waitKey(1000) == 27) {</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">            }</div><div class="line">        }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">ind = 0</div><div class="line"><span class="keywordflow">while</span> <span class="keyword">True</span>:</div><div class="line">    update_map(ind, map_x, map_y)</div><div class="line">    ind = (ind + 1) % 4</div><div class="line">    dst = <a class="code" href="../../da/d54/group__imgproc__transform.html#gab75ef31ce5cdfb5c44b6da5f3b908ea4">cv.remap</a>(src, map_x, map_y, cv.INTER_LINEAR)</div><div class="line">    <a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(window_name, dst)</div><div class="line">    c = <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">cv.waitKey</a>(1000)</div><div class="line">    <span class="keywordflow">if</span> c == 27:</div><div class="line">        <span class="keywordflow">break</span></div></div><!-- fragment --> </div> <ul>
<li><p class="startli">The function that applies the remapping is <a class="el" href="../../da/d54/group__imgproc__transform.html#gab75ef31ce5cdfb5c44b6da5f3b908ea4">cv::remap</a> . We give the following arguments:</p><ul>
<li><b>src</b>: Source image</li>
<li><b>dst</b>: Destination image of same size as <em>src</em></li>
<li><b>map_x</b>: The mapping function in the x direction. It is equivalent to the first component of \(h(i,j)\)</li>
<li><b>map_y</b>: Same as above, but in y direction. Note that <em>map_y</em> and <em>map_x</em> are both of the same size as <em>src</em></li>
<li><b>INTER_LINEAR</b>: The type of interpolation to use for non-integer pixels. This is by default.</li>
<li><b>BORDER_CONSTANT</b>: Default</li>
</ul>
<p class="startli">How do we update our mapping matrices <em>mat_x</em> and <em>mat_y</em>? Go on reading:</p>
</li>
<li><b>Updating the mapping matrices:</b> We are going to perform 4 different mappings:<ol type="1">
<li>Reduce the picture to half its size and will display it in the middle: <p class="formulaDsp">
\[h(i,j) = ( 2 \times i - src.cols/2 + 0.5, 2 \times j - src.rows/2 + 0.5)\]
</p>
 for all pairs \((i,j)\) such that: \(\dfrac{src.cols}{4}&lt;i&lt;\dfrac{3 \cdot src.cols}{4}\) and \(\dfrac{src.rows}{4}&lt;j&lt;\dfrac{3 \cdot src.rows}{4}\)</li>
<li>Turn the image upside down: \(h( i, j ) = (i, src.rows - j)\)</li>
<li>Reflect the image from left to right: \(h(i,j) = ( src.cols - i, j )\)</li>
<li>Combination of b and c: \(h(i,j) = ( src.cols - i, src.rows - j )\)</li>
</ol>
</li>
</ul>
<p>This is expressed in the following snippet. Here, <em>map_x</em> represents the first coordinate of <em>h(i,j)</em> and <em>map_y</em> the second coordinate.</p>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line"><span class="keywordtype">void</span> update_map( <span class="keywordtype">int</span> &amp;ind, Mat &amp;map_x, Mat &amp;map_y )</div><div class="line">{</div><div class="line">    <span class="keywordflow">for</span>( <span class="keywordtype">int</span> i = 0; i &lt; map_x.rows; i++ )</div><div class="line">    {</div><div class="line">        <span class="keywordflow">for</span>( <span class="keywordtype">int</span> j = 0; j &lt; map_x.cols; j++ )</div><div class="line">        {</div><div class="line">            <span class="keywordflow">switch</span>( ind )</div><div class="line">            {</div><div class="line">            <span class="keywordflow">case</span> 0:</div><div class="line">                <span class="keywordflow">if</span>( j &gt; map_x.cols*0.25 &amp;&amp; j &lt; map_x.cols*0.75 &amp;&amp; i &gt; map_x.rows*0.25 &amp;&amp; i &lt; map_x.rows*0.75 )</div><div class="line">                {</div><div class="line">                    map_x.at&lt;<span class="keywordtype">float</span>&gt;(i, j) = 2*( j - map_x.cols*0.25f ) + 0.5f;</div><div class="line">                    map_y.at&lt;<span class="keywordtype">float</span>&gt;(i, j) = 2*( i - map_x.rows*0.25f ) + 0.5f;</div><div class="line">                }</div><div class="line">                <span class="keywordflow">else</span></div><div class="line">                {</div><div class="line">                    map_x.at&lt;<span class="keywordtype">float</span>&gt;(i, j) = 0;</div><div class="line">                    map_y.at&lt;<span class="keywordtype">float</span>&gt;(i, j) = 0;</div><div class="line">                }</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">            <span class="keywordflow">case</span> 1:</div><div class="line">                map_x.at&lt;<span class="keywordtype">float</span>&gt;(i, j) = (<span class="keywordtype">float</span>)j;</div><div class="line">                map_y.at&lt;<span class="keywordtype">float</span>&gt;(i, j) = (<span class="keywordtype">float</span>)(map_x.rows - i);</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">            <span class="keywordflow">case</span> 2:</div><div class="line">                map_x.at&lt;<span class="keywordtype">float</span>&gt;(i, j) = (<span class="keywordtype">float</span>)(map_x.cols - j);</div><div class="line">                map_y.at&lt;<span class="keywordtype">float</span>&gt;(i, j) = (<span class="keywordtype">float</span>)i;</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">            <span class="keywordflow">case</span> 3:</div><div class="line">                map_x.at&lt;<span class="keywordtype">float</span>&gt;(i, j) = (<span class="keywordtype">float</span>)(map_x.cols - j);</div><div class="line">                map_y.at&lt;<span class="keywordtype">float</span>&gt;(i, j) = (<span class="keywordtype">float</span>)(map_x.rows - i);</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">            <span class="keywordflow">default</span>:</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">            } <span class="comment">// end of switch</span></div><div class="line">        }</div><div class="line">    }</div><div class="line">    ind = (ind+1) % 4;</div><div class="line">}</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">    <span class="keyword">private</span> <span class="keywordtype">void</span> updateMap() {</div><div class="line">        <span class="keywordtype">float</span> buffX[] = <span class="keyword">new</span> <span class="keywordtype">float</span>[(int) (mapX.total() * mapX.channels())];</div><div class="line">        mapX.get(0, 0, buffX);</div><div class="line"></div><div class="line">        <span class="keywordtype">float</span> buffY[] = <span class="keyword">new</span> <span class="keywordtype">float</span>[(int) (mapY.total() * mapY.channels())];</div><div class="line">        mapY.get(0, 0, buffY);</div><div class="line"></div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; mapX.rows(); i++) {</div><div class="line">            <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j &lt; mapX.cols(); j++) {</div><div class="line">                <span class="keywordflow">switch</span> (ind) {</div><div class="line">                <span class="keywordflow">case</span> 0:</div><div class="line">                    <span class="keywordflow">if</span>( j &gt; mapX.cols()*0.25 &amp;&amp; j &lt; mapX.cols()*0.75 &amp;&amp; i &gt; mapX.rows()*0.25 &amp;&amp; i &lt; mapX.rows()*0.75 ) {</div><div class="line">                        buffX[i*mapX.cols() + j] = 2*( j - mapX.cols()*0.25f ) + 0.5f;</div><div class="line">                        buffY[i*mapY.cols() + j] = 2*( i - mapX.rows()*0.25f ) + 0.5f;</div><div class="line">                    } <span class="keywordflow">else</span> {</div><div class="line">                        buffX[i*mapX.cols() + j] = 0;</div><div class="line">                        buffY[i*mapY.cols() + j] = 0;</div><div class="line">                    }</div><div class="line">                    <span class="keywordflow">break</span>;</div><div class="line">                <span class="keywordflow">case</span> 1:</div><div class="line">                    buffX[i*mapX.cols() + j] = j;</div><div class="line">                    buffY[i*mapY.cols() + j] = mapY.rows() - i;</div><div class="line">                    <span class="keywordflow">break</span>;</div><div class="line">                <span class="keywordflow">case</span> 2:</div><div class="line">                    buffX[i*mapX.cols() + j] = mapY.cols() - j;</div><div class="line">                    buffY[i*mapY.cols() + j] = i;</div><div class="line">                    <span class="keywordflow">break</span>;</div><div class="line">                <span class="keywordflow">case</span> 3:</div><div class="line">                    buffX[i*mapX.cols() + j] = mapY.cols() - j;</div><div class="line">                    buffY[i*mapY.cols() + j] = mapY.rows() - i;</div><div class="line">                    <span class="keywordflow">break</span>;</div><div class="line">                <span class="keywordflow">default</span>:</div><div class="line">                    <span class="keywordflow">break</span>;</div><div class="line">                }</div><div class="line">            }</div><div class="line">        }</div><div class="line">        mapX.put(0, 0, buffX);</div><div class="line">        mapY.put(0, 0, buffY);</div><div class="line">        ind = (ind+1) % 4;</div><div class="line">    }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line"><span class="keyword">def </span>update_map(ind, map_x, map_y):</div><div class="line">    <span class="keywordflow">if</span> ind == 0:</div><div class="line">        <span class="keywordflow">for</span> i <span class="keywordflow">in</span> range(map_x.shape[0]):</div><div class="line">            <span class="keywordflow">for</span> j <span class="keywordflow">in</span> range(map_x.shape[1]):</div><div class="line">                <span class="keywordflow">if</span> j &gt; map_x.shape[1]*0.25 <span class="keywordflow">and</span> j &lt; map_x.shape[1]*0.75 <span class="keywordflow">and</span> i &gt; map_x.shape[0]*0.25 <span class="keywordflow">and</span> i &lt; map_x.shape[0]*0.75:</div><div class="line">                    map_x[i,j] = 2 * (j-map_x.shape[1]*0.25) + 0.5</div><div class="line">                    map_y[i,j] = 2 * (i-map_y.shape[0]*0.25) + 0.5</div><div class="line">                <span class="keywordflow">else</span>:</div><div class="line">                    map_x[i,j] = 0</div><div class="line">                    map_y[i,j] = 0</div><div class="line">    <span class="keywordflow">elif</span> ind == 1:</div><div class="line">        <span class="keywordflow">for</span> i <span class="keywordflow">in</span> range(map_x.shape[0]):</div><div class="line">            map_x[i,:] = [x <span class="keywordflow">for</span> x <span class="keywordflow">in</span> range(map_x.shape[1])]</div><div class="line">        <span class="keywordflow">for</span> j <span class="keywordflow">in</span> range(map_y.shape[1]):</div><div class="line">            map_y[:,j] = [map_y.shape[0]-y <span class="keywordflow">for</span> y <span class="keywordflow">in</span> range(map_y.shape[0])]</div><div class="line">    <span class="keywordflow">elif</span> ind == 2:</div><div class="line">        <span class="keywordflow">for</span> i <span class="keywordflow">in</span> range(map_x.shape[0]):</div><div class="line">            map_x[i,:] = [map_x.shape[1]-x <span class="keywordflow">for</span> x <span class="keywordflow">in</span> range(map_x.shape[1])]</div><div class="line">        <span class="keywordflow">for</span> j <span class="keywordflow">in</span> range(map_y.shape[1]):</div><div class="line">            map_y[:,j] = [y <span class="keywordflow">for</span> y <span class="keywordflow">in</span> range(map_y.shape[0])]</div><div class="line">    <span class="keywordflow">elif</span> ind == 3:</div><div class="line">        <span class="keywordflow">for</span> i <span class="keywordflow">in</span> range(map_x.shape[0]):</div><div class="line">            map_x[i,:] = [map_x.shape[1]-x <span class="keywordflow">for</span> x <span class="keywordflow">in</span> range(map_x.shape[1])]</div><div class="line">        <span class="keywordflow">for</span> j <span class="keywordflow">in</span> range(map_y.shape[1]):</div><div class="line">            map_y[:,j] = [map_y.shape[0]-y <span class="keywordflow">for</span> y <span class="keywordflow">in</span> range(map_y.shape[0])]</div></div><!-- fragment --> </div> <h2>Result </h2>
<ol type="1">
<li><p class="startli">After compiling the code above, you can execute it giving as argument an image path. For instance, by using the following image:</p>
<div class="image">
<img src="../../Remap_Tutorial_Original_Image.jpg" alt="Remap_Tutorial_Original_Image.jpg"/>
</div>
</li>
<li><p class="startli">This is the result of reducing it to half the size and centering it:</p>
<div class="image">
<img src="../../Remap_Tutorial_Result_0.jpg" alt="Remap_Tutorial_Result_0.jpg"/>
</div>
</li>
<li><p class="startli">Turning it upside down:</p>
<div class="image">
<img src="../../Remap_Tutorial_Result_1.jpg" alt="Remap_Tutorial_Result_1.jpg"/>
</div>
</li>
<li><p class="startli">Reflecting it in the x direction:</p>
<div class="image">
<img src="../../Remap_Tutorial_Result_2.jpg" alt="Remap_Tutorial_Result_2.jpg"/>
</div>
</li>
<li><p class="startli">Reflecting it in both directions:</p>
<div class="image">
<img src="../../Remap_Tutorial_Result_3.jpg" alt="Remap_Tutorial_Result_3.jpg"/>
</div>
 </li>
</ol>
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:35 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
